//
//  API.swift
//  Box
//
//  Created by mn on 2025/3/28.
//

import Moya

enum API {
    case getCode(phone: String)
    case login(phone: String,code: String)
    case logout
    case deleteUser
    case getMyAddressList
    case addAddress(addressPath: String, detailAddress: String, mobile: String, name: String, defaultSelected: Int)
    case deleteAddress(id: String)
    case getDefaultAddress
    case updateAddress(id: String, addressPath: String, detailAddress: String, mobile: String, name: String, defaultSelected: Int)
    case updateDefaultAddress(id: String)
    case getNewRegionInfo
    case getGoodsTypeList
    case getGoodsListByTopTypeId(goodsTopTypeId: String)
    case getRecommendGoodsList
    case getGoodsDetail(goodsId: String)
    case buyGoods(goodsId: String,count: Int)
    case getMyStoreList
    case getOrderFreight(storeIdList: [String])
    case createOrder(storeIdList: [String],addressId: String)
    case orderList(page: Int,status: Int)
    case orderDetail(orderId: String)
    case getUserInfo
    case createPayOrder(amount: Int)
    
}

extension API: TargetType {
    var baseURL: URL {
        return URL(string: "https://home.cqbbx.cn/api")!
    }

    var path: String {
        switch self {
        case .getCode(let phone):
            return "/shopServer/user/getCode/\(phone)"
        case .login:
            return "/shopServer/user/loginOrRegister"
        case .logout:
            return "/shopServer/user/loginOut"
        case .deleteUser:
            return "/shopServer/user/deleteUser"
        case .getMyAddressList:
            return "/shopServer/address/getMyAddressList"
        case .addAddress:
            return "/shopServer/address/addAddress"
        case .deleteAddress:
            return "/shopServer/address/deleteAddress"
        case .getDefaultAddress:
            return "/shopServer/address/getDefaultAddress"
        case .updateAddress:
            return "/shopServer/address/updateAddress"
        case .updateDefaultAddress:
            return "/shopServer/address/updateDefaultAddress"
        case .getNewRegionInfo:
            return "/shopServer/address/getNewRegionInfo"
        case .getGoodsTypeList:
            return "/shopServer/goods/getGoodsTypeList"
        case .getGoodsListByTopTypeId:
            return "/shopServer/goods/getGoodsListByTopTypeId"
        case .getRecommendGoodsList:
            return "/shopServer/goods/getRecommendGoodsList"
        case .getGoodsDetail(let goodsId):
            return "/shopServer/goods/getGoodsDetail/\(goodsId)"
        case .buyGoods:
            return "/shopServer/goods/buy"
        case .getMyStoreList:
            return "/shopServer/store/getMyStoreList"
        case .getOrderFreight:
            return "/shopServer/order/getOrderFreight"
        case .createOrder:
            return "/shopServer/order/createOrder"
        case .orderList:
            return "/shopServer/order/getList"
        case .orderDetail:
            return "/shopServer/order/getOrderDetail"
        case .getUserInfo:
            return "/shopServer/user/getUserInfo"
        case .createPayOrder:
            return "shopServer/pay/createPayOrder"
        }
    }

    var method: Moya.Method {
        switch self {
        case .getCode, .getMyAddressList, .getDefaultAddress, .getNewRegionInfo, .getGoodsTypeList, .getGoodsListByTopTypeId, .getRecommendGoodsList,.getGoodsDetail, .getMyStoreList,.orderList,.orderDetail,.getUserInfo:
            return .get
        default :
            return .post
        }
    }

    var task: Task {
        switch self {
        case .getCode, .logout, .deleteUser, .getMyAddressList, .getDefaultAddress, .getNewRegionInfo, .getGoodsTypeList, .getRecommendGoodsList,.getGoodsDetail, .getMyStoreList,.getUserInfo:
            return .requestPlain
        case .login(let phone, let code):
            let parameters: [String: String] = [
                "phone": phone,
                "code": code
            ]
            return .requestParameters(parameters: parameters, encoding: URLEncoding.httpBody)
        case .addAddress(let addressPath, let detailAddress, let mobile, let name, let defaultSelected):
            let parameters: [String: Any] = [
                "addressPath": addressPath,
                "detailAddress": detailAddress,
                "mobile": mobile,
                "name": name,
                "defaultSelected": defaultSelected
            ]
            return .requestParameters(parameters: parameters, encoding: URLEncoding.httpBody)
        case .deleteAddress(let id):
            let parameters: [String: Any] = [
                "id": id
            ]
            return .requestParameters(parameters: parameters, encoding: URLEncoding.httpBody)
        case .updateAddress(let id, let addressPath, let detailAddress, let mobile, let name, let defaultSelected):
            let parameters: [String: Any] = [
                "id": id,
                "addressPath": addressPath,
                "detailAddress": detailAddress,
                "mobile": mobile,
                "name": name,
                "defaultSelected": defaultSelected
            ]
            return .requestParameters(parameters: parameters, encoding: URLEncoding.httpBody)
        case .updateDefaultAddress(let id):
            return .requestParameters(parameters: ["id": id], encoding: URLEncoding.httpBody)
        case .getGoodsListByTopTypeId(let goodsTopTypeId):
            let parameters: [String: Any] = ["goodsTopTypeId": goodsTopTypeId]
            return .requestParameters(parameters: parameters, encoding: URLEncoding.default)
        case let .buyGoods(goodsId, count):
            let parameters: [String: Any] = ["goodsId": goodsId,
                                             "count": count]
            return .requestParameters(parameters: parameters, encoding: URLEncoding.httpBody)
        case let .getOrderFreight(storeIdList):
            return .requestParameters(parameters: ["storeIdList":storeIdList],
                                      encoding: URLEncoding.httpBody)
        case let .createOrder(storeIdList, addressId):
            let parameters: [String: Any] = ["storeIdList": storeIdList,
                                             "addressId": addressId]
            return .requestParameters(parameters: parameters, encoding: URLEncoding.httpBody)
        case let .orderList(page, status):
            
            let parameters = (status == 0) ? ["page": page] : ["page": page,"status": status]
            return .requestParameters(parameters: parameters, encoding: URLEncoding.default)
        case let .orderDetail(orderId):
            return .requestParameters(parameters: ["orderId": orderId], encoding: URLEncoding.default)
        case let .createPayOrder(amount):
            return .requestParameters(parameters: ["amount": amount,
                                                   "payType":1],
                                      encoding: URLEncoding.httpBody)
        }
    }

    var headers: [String: String]? {
        guard let user = UserManager.shared.currentUser else {
            return ["Content-Type": "application/x-www-form-urlencoded"]
        }
        return ["Content-Type":
                    "application/x-www-form-urlencoded",
                "token":user.token]
    }
}
